package com.tool.cn.util;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.util.StatusPrinter;

/**
 * logback加载配置文件
 * 
 * @author wangzh
 * @date 2018年7月30日 下午2:08:10
 */
public final class LogbackInit {
	
	private static Logger log= LoggerFactory.getLogger(LogbackInit.class);
	
	 /**
     * 设置logback.xml配置文件并加载
     *
     * @param configFilepathName 配置文件路径名
     */
    public static void initLogback(String configFilepathName) {
        File file = new File(configFilepathName);
        if(!file.exists()){
        	log.error("Not Found File:"+configFilepathName);
        	return;
        }
        
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator joranConfigurator = new JoranConfigurator();
        joranConfigurator.setContext(loggerContext);
        loggerContext.reset();
        loggerContext.putProperty("other", "路径测试"); //xml中 ${other调用}
        try {
            joranConfigurator.doConfigure(file);
        } catch (Exception e) {
        	log.error("Load logback config file error. Message: ", e.getMessage());
        }
        StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
    }
}
